   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 
   
OAA_CI.headland = OAA_CI.headland || {}; 

/** ================================================================
*
* Headings and Landmarks Module initialization
*
* =============================================================== */


OAA_CI.headings_last_index = 0;
OAA_CI.landmarks_last_index = 0;
OAA_CI.main_last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes landmarks module
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.landmarks_nls_strings = document.getElementById("ID_STRINGBUNDLE_LANDMARKS");        
  OpenAjax.a11y.console("Loading landmark strings: " + OAA_CI.landmarks_nls_strings);

  OAA_CI.headings_nls_strings = document.getElementById("ID_STRINGBUNDLE_HEADINGS");        
  OpenAjax.a11y.console("Loading heading strings: " + OAA_CI.headings_nls_strings);

  OAA_CI.headland.onShowHeadingsAndLandmarksCommand();
  
}; 

/** ================================================================
*
* onShowHeadingsAndLandmarksCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with heading and landmark information
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onShowHeadingsAndLandmarksCommand = function(e) {

    
  function addHeadingsAndLandmarkChildren(xul_treechildren, child_elements) {
  
    var i = 0;
    var ce;
    var child_elements_len = child_elements.length;
    
    var xul_treeitem;
    var xul_sub_treechildren;
    
    for (i=0; i<child_elements_len; i++) {
    
      ce = child_elements[i];
    
      xul_treeitem = OAA_CI.createXULElement('treeitem', []);

      if (ce.level) {
        OAA_CI.addTreeRow(xul_treeitem, [ce.dom_element.tag_name, ce.level, ce.name], ce.cache_id );
        OAA_CI.headings_and_landmarks.push(ce);
      }
      else {
        OAA_CI.addTreeRow(xul_treeitem, [ce.dom_element.tag_name, ce.dom_element.role, ce.name], ce.cache_id );
        OAA_CI.headings_and_landmarks.push(ce);
      
        if (ce.child_elements.length) {
      
          xul_treeitem.setAttribute("container", "true");
          xul_treeitem.setAttribute("open", "true");

          xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);

          addHeadingsAndLandmarkChildren(xul_sub_treechildren, ce.child_elements);
         
          xul_treeitem.appendChild(xul_sub_treechildren);      
        }
      }        
      xul_treechildren.appendChild(xul_treeitem);
    } // loop
    
  } // end function

  OAA_CI.evaluateDocument();   
  
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var child_elements = headings_landmarks_cache.child_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_HEADINGS_LANDMARKS');
  
  OAA_CI.removeChildNodes(xul_treechildren);
  OAA_CI.headings_and_landmarks = [];

  addHeadingsAndLandmarkChildren(xul_treechildren, child_elements);
    
  OAA_CI.updateXULTime(start_time);

  
};  


/** ================================================================
*
* onHeadingsAndLandmarkElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the LandmarkCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onHeadingsAndLandmarksElementItemsSelectChanged = function (event) {

  var le;
  var he;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var xul_tree = document.getElementById('ID_TREE_HEADINGS_LANDMARKS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Heading and landmark cache id: " + cache_id );

    le = headings_landmarks_cache.getLandmarkElementByCacheId(cache_id);
    
    if (le) {
      OAA_CI.showCacheItemProperties(le);
    }
    else {    
      he = headings_landmarks_cache.getHeadingElementByCacheId(cache_id);
      if (he) {
       OAA_CI.showCacheItemProperties(he);
      } 
    }
  } 
 
};

/** ================================================================
*
* onShowMainCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with titling information
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onShowMainCommand = function(e) {

  function addMainChildren(xul_treechildren, main_elements) {
  
    var i = 0;
    var me;
    var main_elements_len = main_elements.length;
    
    var xul_treeitem;
    var xul_sub_treechildren;
    
    for (i=0; i<main_elements_len; i++) {
    
      me = main_elements[i];
    
      xul_treeitem = OAA_CI.createXULElement('treeitem', []);

      if (me.level) {
        OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, me.level, me.name], me.cache_id );
      }
      else {
        OAA_CI.addTreeRow(xul_treeitem, [me.dom_element.tag_name, me.dom_element.role, me.name], me.cache_id );
      
        if (me.main_elements.length) {
      
          xul_treeitem.setAttribute("container", "true");
          xul_treeitem.setAttribute("open", "true");

          xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);

          addMainChildren(xul_sub_treechildren, me.main_elements);
         
          xul_treeitem.appendChild(xul_sub_treechildren);      
        }
      }        
      xul_treechildren.appendChild(xul_treeitem);
    } 
  }
  
  OAA_CI.evaluateDocument();   
  
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var child_main_elements = headings_landmarks_cache.child_main_elements;
 
  var start_time = new Date().getTime();

  var xul_title_textbox = document.getElementById('ID_TITLE_TEXTBOX');

  xul_title_textbox.value = OAA_CI.evaluation.dom_cache.document.title;

  var xul_treechildren = document.getElementById('ID_TREECHILDREN_MAIN');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  addMainChildren(xul_treechildren, child_main_elements);
    
  OAA_CI.updateXULTime(start_time);
      
};  


/** ================================================================
*
* onMainElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the LandmarkCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onMainElementItemsSelectChanged = function (event) {

  var me;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var xul_tree = document.getElementById('ID_TREE_MAIN');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Main cache id: " + cache_id );

    me = headings_landmarks_cache.getMainElementByCacheId(cache_id);
    
    if (me) {
      OAA_CI.showCacheItemProperties(me);
    }
    
  } 
 
};

/** ================================================================
*
* onShowHeadingsCommand
*
* @desc 
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onShowHeadingsCommand = function(e) {

  var i;
  var he;

  OAA_CI.evaluateDocument();   
  
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  headings_landmarks_cache.sortHeadingElements('document_order', true);
  
  var heading_elements = headings_landmarks_cache.heading_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_HEADINGS');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var heading_elements_len = heading_elements.length;
  
  OpenAjax.a11y.console("Number of headings: " + heading_elements_len);
  
  if (heading_elements_len) {
    for (i=0; i<heading_elements_len; i++) {
      he = heading_elements[i];
      OAA_CI.addTreeItem(xul_treechildren, [he.document_order, he.level, he.name], he.cache_id);
    }  // end loop
  }  
  else {
    var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    
};  


/** ================================================================
*
* onHeadingElementItemsSelectChanged
*
* @desc Updates properties currently selected HeadingElement
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onHeadingElementItemsSelectChanged = function (event) {

  var he;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var xul_tree = document.getElementById('ID_TREE_HEADINGS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Heading cache id: " + cache_id );

    he = headings_landmarks_cache.getHeadingElementByCacheId(cache_id);
    
    if (he) {
      OAA_CI.showCacheItemProperties(he);
    }
    
  } 
 
};

/** ================================================================
*
* onShowLandmarksCommand
*
* @desc 
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onShowLandmarksCommand = function(e) {

  var i;
  var le;

  OAA_CI.evaluateDocument();   
  
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  headings_landmarks_cache.sortHeadingElements('document_order', true);
  
  var landmark_elements = headings_landmarks_cache.landmark_elements;

  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_LANDMARKS');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var landmark_elements_len = landmark_elements.length;
  
  OpenAjax.a11y.console("Number of landmarks: " + landmark_elements_len);
  
  if (landmark_elements_len) {
    for (i=0; i<landmark_elements_len; i++) {
      le = landmark_elements[i];
      OAA_CI.addTreeItem(xul_treechildren, [le.document_order, le.dom_element.tag_name, le.dom_element.role, le.name], le.cache_id);
    }  // end loop
  }  
  else {
    var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","","","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    
};  


/** ================================================================
*
* onLandmarkElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected LandmarkElement
*
* @return nothing
*
* =============================================================== */

OAA_CI.headland.onLandmarkElementItemsSelectChanged = function (event) {

  var le;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var headings_landmarks_cache = OAA_CI.evaluation.dom_cache.headings_landmarks_cache;  
  var xul_tree = document.getElementById('ID_TREE_LANDMARKS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Landmark cache id: " + cache_id );

    le = headings_landmarks_cache.getLandmarkElementByCacheId(cache_id);
    
    if (le) {
      OAA_CI.showCacheItemProperties(le);
    }
    
  } 
 
};




